<?php

namespace app\api\controller;

use app\admin\model\Article;
use app\admin\model\Banner;
use app\admin\model\Collect;
use app\admin\model\Company;
use app\admin\model\History;
use app\admin\model\Job;
use app\admin\model\Mask;
use app\common\controller\Api;
use app\common\library\Image;
use app\common\model\Config;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ["init",'search','job','banner','company'];
    protected $noNeedRight = ['*'];
    protected $cateMap=[
        '专业'=>"category_name",
        '岗位'=>"job_cate",
        '企业'=>"companey_name",
        '链接'=>"link",
    ];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    public function init()
    {
        $jobModel=new Job();
        $companeyList=$jobModel->groupAndCount("companey_name",10);
        foreach ($companeyList as $k=>$v){
            $companeyList[$k]['is_collect']=Collect::where('user_id',$this->auth->id)->where('job_id',$v['id'])->count();
        }
        $list['companeyList']=$companeyList;
        $list['articleList']=Article::order('createtime desc')->limit(10)->select();
        $jobCateList=$jobModel->groupAndCount("job_cate",10);
        foreach ($jobCateList as $kj=>$vj){
            $jobCateList[$kj]['is_collect']=Collect::where('user_id',$this->auth->id)->where('job_id',$vj['id'])->count();
        }
        $list['jobCateList']=$jobCateList;
        $industryList=$jobModel->groupAndCount("industry",10);
        foreach ($jobCateList as $ki=>$vi){
            $jobCateList[$ki]['is_collect']=Collect::where('user_id',$this->auth->id)->where('job_id',$vi['id'])->count();
        }
        $list['industryList']=$industryList;
        $list['website']=Config::where('name','in',['name','beian'])->column('name,value');
        $this->success('ok',$list);
    }
    public function search()
    {
        $keyword=input('keyword',"");
        $cate=input('cate',"");
        $filed=$cate?$this->cateMap[$cate]:"";
        $where=[];
        if($keyword){
            $where['title']=['like',"%$keyword%"];
        }
        if($filed=="link"){
            $parsed = parse_url($keyword);
            $domain = $parsed['host'] ?? "";
            $where['from_url'] = ['like',"%$domain%"];
        }elseif($filed!="jobcate" && $filed){
            unset($where['title']);
            $where[$filed]=['like',"%$keyword%"];
        }
        $jobCate=input('job_cate',"");
        if($jobCate){
            $where['job_cate']=$jobCate;
        }
        $nature=input('nature',"");
        if($nature){
            $where['nature']=$nature;
        }
        $wherestr="";
        $workLocation=input('work_localtion',"");
        if($workLocation){
            $location=explode(",",$workLocation);
            $locationStr="(";
            foreach ($location as $key => $value) {
                if(!$value) continue;
                $locationStr.=($locationStr?" or work_localtion like '%$value%'":"(work_localtion like '%$value%'");
            }
            $locationStr=$locationStr?$locationStr.")":"";
            $wherestr.=$locationStr;
       }
        $education=input('education',"");
        if($education){
            $where['education']=$education;
        }
        $company_nature=input('company_nature',"");
        if($company_nature){
            $where['company_nature']=["in",explode(",",$company_nature)];
        }
        $add_time=input('add_time',"");
        if($add_time){
            $addTime=explode("--",$add_time);
            $where['add_time']=["between", [strtotime($addTime[0]),strtotime($addTime[1])]];
        }

        $company_id=input('company_id',"");
        if($company_id){
            $where['company_id']=$company_id;
        }else{
            $mask=Mask::where('user_id',$this->auth->id)->column('company_id');
            if($mask){
                $where['company_id']=["not in",$mask];
            }
        }
        setLog(__METHOD__,$where);
        $list=Job::where($where)->where($wherestr)->order('createtime desc')->paginate(10)->each(function($item){
            $item['is_collect']=Collect::where(['user_id'=>$this->auth->id,'job_id'=>$item['id']])->count();
            return $item;
        });
        $this->success('ok',$list);
    }
    public function job()
    {
        $job_id=input('job_id',0);
        $job=Job::get($job_id);
        if(!$job->company_id){
            $company=Company::create([
                'name'=>$job->companey_name,
                'info'=>$job->companey_name,
                'pid'=>0,
                'pname'=>"",
                'logo'=>Image::letterLog($job->companey_name),
            ]);
            Job::where('companey_name',$job->companey_name)->update(['company_id'=>$company->id]);
        }
        $job->is_collect=Collect::where('job_id',$job_id)->where('user_id',$this->auth->id)->count();
        $job->is_mask=Mask::where('job_id',$job_id)->where('user_id',$this->auth->id)->count();
        $jobList=Job::where('company_id',$job->company_id)->order('id desc')->select();
        $this->success('ok',['detail'=>$job,'job_list'=>$jobList]);
    }
    /*收藏岗位*/
    public function collect()
    {
        $job_id=input('job_id',0);
        $job=Job::get($job_id);
        if(!$job) $this->error('错误');
        $collect=Collect::get(['user_id'=>$this->auth->id,'job_id'=>$job_id]);
        if($collect) {
            $collect->delete();
        }else{
            Collect::create([
                'user_id'=>$this->auth->id,
                'job_id'=>$job_id,
            ]);
        }
        $this->success('ok');
    }
    public function collectList()
    {
        $list=Collect::with('job')->where('user_id',$this->auth->id)->order('createtime desc')->paginate(10);
        $this->success('ok',$list);
    }
    public function mask()
    {
        $id=input('id',0);
        $type=input('type',"company");
        if($type=="company"){
            $list=Company::get($id);
            $filed="company_id";
        }else{
            $list=Job::get($id);
            $filed="job_id";
        }
        if(!$list) $this->error('失败');
        $mask=Mask::get(['user_id'=>$this->auth->id,$filed=>$id]);
        if($mask) {
            $mask->delete();
        }else{
            Mask::create([
                $filed=>$id,
                'user_id'=>$this->auth->id
            ]);
        }
        $this->success('ok');
    }
    public function maskList()
    {
        $keyword=input('keyword',"");
        $type=input('type',"company");
        if($type=="company"){
            $list=Mask::with(['company'=>function($query) use ($keyword){
                $query->where('name','like',"%$keyword%");
            }])->where('user_id',$this->auth->id)->where('company_id',">",0)->paginate(15);
        }else{
            $list=Mask::with(['job'=>function($query) use ($keyword){
                $query->where('name','like',"%$keyword%");
            }])->where('user_id',$this->auth->id)->where('job',">",0)->paginate(15);
        }
        $this->success('ok',$list);
    }
    /*企业信息*/
    public function company()
    {
        $id=input('company_id',0);
        $companeyModel=new Company();
        $list=$companeyModel::get($id);
        $job=Job::where('company_id',$id)->order('id desc')->paginate(10)->each(function($item){
            $item['is_collect']=Collect::where('job_id',$item['id'])->count();
            return $item;
        });
        $yy=$companeyModel::where('pid',$id);
        if($list['pid']){
            $relate=$yy->whereOr('pid',$list['pid'])->select();
        }else{
            $relate=$yy->select();
        }
        $list->is_mask=Mask::where('company_id',$id)->where('user_id',$this->auth->id)->count();
        $this->success('ok',['detail'=>$list,'job_list'=>$job,'relate'=>$relate]);
    }
    public function setHistory()
    {
        $job_id=input('job_id',0);
        if(!$job_id) $this->error('缺少参数');
        $user_id=$this->auth->id;
        $historyModel=new History();
        $history=$historyModel::get(['job_id'=>$job_id,'user_id'=>$user_id]);
        if(!$history){
            $historyModel::create(['job_id'=>$job_id,'user_id'=>$user_id]);
        }
        $this->success('ok');
    }
    public function history()
    {
//        $job_id=input('job_id',0);
        $type=input('type',"my");
        if($type=="my"){
            $list=History::with('job')->where('user_id',$this->auth->id)->paginate(10);
        }else{
            $list=History::with('job')->where('user_id',"<>",$this->auth->id)->paginate(10);
        }
        $this->success('ok',['list'=>$list]);
    }
    
}
